using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Voodoo.Sauce.Core;
using Voodoo.Sauce.Internal.Extension;
using Voodoo.Sauce.Internal.IntegrationCheck;

namespace Voodoo.Sauce.Internal.Analytics
{
	public class ABTestIntegrationCheck : IIntegrationCheck
	{
		private const string SETTINGS_AB_TESTING_REMOTELY_DEACTIVATED_ERROR = "You have added A/B Tests in VoodooSettings 'Running AB Tests' section but VoodooAnalytics is deactivated via VoodooKitchen. Please contact Voodoo if you believe this is a mistake.";

		private const string SETTINGS_LEGACY_AB_TESTING_IGNORED_WARNING = "A/B Tests set in VoodooSettings 'Running AB Tests' section will be ignored since RemoteConfig is enabled via VoodooKitchen. Please contact Voodoo if you believe this is a mistake or remove your running A/B tests";

		private const string SETTINGS_VOODOO_ANALYTICS_AB_TESTING_DEACTIVATED_ERROR = "VoodooAnalytics must be activated via VoodooKitchen for RemoteConfig AB Tests to work. Please contact Voodoo if you believe this is a mistake.";

		private const string SETTINGS_DEACTIVATE_MIXPANEL_WARNING = "Mixpanel is expensive and should only be used on a limited volume of users. We encourage you to deactivate Mixpanel in VoodooKitchen.";

		private const string SETTINGS_AB_TESTS_CUSTOM_COUNTRIES_REMOTELY_DEACTIVATED_ERROR = "You have added countries under 'Custom AB Tests Country Codes' section but A/B testing is deactivated via VoodooKitchen for countries other than the 'US'. Please contact Voodoo if you believe this is a mistake";

		private const string SETTINGS_AB_TESTS_EMPTY_CUSTOM_COUNTRIES_ERROR = "You have added A/B Tests under 'Running AB Tests' section but the 'Custom AB Tests Country Codes' is empty.";

		private const string SETTINGS_AB_TESTS_WRONG_COUNTRY_CODE_ERROR = "You have placed an invalid country code {0} in your 'Custom AB Tests Country Codes'.";

		private const string SETTINGS_AB_TESTS_TOTAL_PERCENT_EXCEEDS_MAX_ERROR = "The total percentage of A/B test users is {0} * {1} = {2}. This exceeds the max value of {3}.";

		private const string SETTINGS_AB_TESTS_NO_NAME_PROVIDED = "You are currently using Legacy A.B Tests. Please enter the name of the current A/B test under Legacy A/B Tests -> LegacyAbTestName";

		public List<IntegrationCheckMessage> IntegrationCheck(VoodooSettings settings)
		{
			List<IntegrationCheckMessage> list = new List<IntegrationCheckMessage>();
			string[] runningABTests = settings.GetRunningABTests();
			bool flag = settings.UseMixpanel();
			bool useRemoteConfig = settings.UseRemoteConfig;
			bool useVoodooAnalytics = settings.UseVoodooAnalytics;
			bool flag2 = runningABTests != null && runningABTests.Length != 0;
			string legacyAbTestName = settings.LegacyAbTestName;
			if (useRemoteConfig)
			{
				if (flag2)
				{
					list.Add(new IntegrationCheckMessage(IntegrationCheckMessage.Type.WARNING, "A/B Tests set in VoodooSettings 'Running AB Tests' section will be ignored since RemoteConfig is enabled via VoodooKitchen. Please contact Voodoo if you believe this is a mistake or remove your running A/B tests"));
				}
				if (!useVoodooAnalytics)
				{
					list.Add(new IntegrationCheckMessage(IntegrationCheckMessage.Type.ERROR, "VoodooAnalytics must be activated via VoodooKitchen for RemoteConfig AB Tests to work. Please contact Voodoo if you believe this is a mistake."));
				}
				return list;
			}
			if (flag2)
			{
				if (string.IsNullOrWhiteSpace(legacyAbTestName))
				{
					list.Add(new IntegrationCheckMessage(IntegrationCheckMessage.Type.ERROR, "You are currently using Legacy A.B Tests. Please enter the name of the current A/B test under Legacy A/B Tests -> LegacyAbTestName"));
				}
				if (!useVoodooAnalytics)
				{
					list.Add(new IntegrationCheckMessage(IntegrationCheckMessage.Type.ERROR, "You have added A/B Tests in VoodooSettings 'Running AB Tests' section but VoodooAnalytics is deactivated via VoodooKitchen. Please contact Voodoo if you believe this is a mistake."));
					return list;
				}
				if (flag)
				{
					list.Add(new IntegrationCheckMessage(IntegrationCheckMessage.Type.WARNING, "Mixpanel is expensive and should only be used on a limited volume of users. We encourage you to deactivate Mixpanel in VoodooKitchen."));
				}
				bool flag3 = settings.IsCustomABTestsCountryCodesEnabled();
				string[] customABTestsCountryCodes = settings.GetCustomABTestsCountryCodes();
				float num = settings.GeMaxPercentOfTotalCohorts();
				float num2 = settings.GeUsersPercentPerCohort();
				if (!flag3 && customABTestsCountryCodes != null && customABTestsCountryCodes.Any((string code) => code.ToLower() != "us"))
				{
					list.Add(new IntegrationCheckMessage(IntegrationCheckMessage.Type.ERROR, "You have added countries under 'Custom AB Tests Country Codes' section but A/B testing is deactivated via VoodooKitchen for countries other than the 'US'. Please contact Voodoo if you believe this is a mistake"));
					return list;
				}
				if (flag3)
				{
					if (customABTestsCountryCodes == null || customABTestsCountryCodes.Length == 0)
					{
						list.Add(new IntegrationCheckMessage(IntegrationCheckMessage.Type.ERROR, "You have added A/B Tests under 'Running AB Tests' section but the 'Custom AB Tests Country Codes' is empty."));
						return list;
					}
					string[] array = customABTestsCountryCodes;
					foreach (string text in array)
					{
						try
						{
							new RegionInfo(text);
						}
						catch (Exception)
						{
							list.Add(new IntegrationCheckMessage(IntegrationCheckMessage.Type.ERROR, "You have placed an invalid country code {0} in your 'Custom AB Tests Country Codes'.", new string[1] { text }));
						}
					}
				}
				int num3 = runningABTests.Length + 1;
				float num4 = num2 * (float)num3;
				if (num4 > num)
				{
					list.Add(new IntegrationCheckMessage(IntegrationCheckMessage.Type.ERROR, "The total percentage of A/B test users is {0} * {1} = {2}. This exceeds the max value of {3}.", new string[4]
					{
						num3.StringValue(),
						num2.StringValue(),
						num4.StringValue(),
						num.StringValue()
					}));
				}
				return list;
			}
			return list;
		}
	}
}
